package com.srcbox.file.util

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.srcbox.file.R
import java.io.File

class ProtocolDialog(
    val context: Context,
    private val isProtoF: File,
    val isHide: Boolean = false,
    val on: (b: Boolean) -> Unit
) {
    private var bottomSheetDialog: BottomSheetDialog? = null
    fun create() {
        bottomSheetDialog = BottomSheetDialog(context)
        val view = LayoutInflater.from(context).inflate(R.layout.protocol_dialog, null, false)
        bottomSheetDialog?.setContentView(view)
        val bottomSheetDialogParent = view.parent as ViewGroup
        bottomSheetDialogParent.setBackgroundResource(android.R.color.transparent)
        bottomSheetDialog?.show()
        bottomSheetDialog?.setCancelable(false)
        val protocolWebView = view.findViewById<WebView>(R.id.protocolWeb)
        protocolWebView.loadData(
            EggIO().readFile(context.assets.open("html/xy.html")),
            "text/html;charset=utf-8",
            "utf-8"
        )
        val protocolNo = view.findViewById<TextView>(R.id.protocolNo)
        if (isHide) protocolNo.visibility = View.GONE else protocolNo.visibility = View.VISIBLE

        view.findViewById<TextView>(R.id.protocolYes).setOnClickListener {
            bottomSheetDialog?.cancel()
            isProtoF.createNewFile()
            on(true)
        }

        protocolNo.setOnClickListener {
            bottomSheetDialog?.cancel()
            on(false)
        }
    }
}